package info.ask4research.web.dto;

import info.ask4research.model.roles.Role;
import info.ask4research.model.toolsAndMaterials.AnnouncementService;
import info.ask4research.model.toolsAndMaterials.ChatService;
import info.ask4research.model.toolsAndMaterials.ForumService;
import info.ask4research.model.toolsAndMaterials.SentMailService;
import info.ask4research.model.toolsAndMaterials.ToolAndMaterial;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

public class ToolsAndMaterialsRolesDTO {

	String roleName;
	int id;
	boolean observer;
	boolean participant;
	boolean recipient;
	
	HashMap<Integer, ToolsAndMaterialsRolesDTO> roles = new HashMap<Integer, ToolsAndMaterialsRolesDTO>();
	List<ToolsAndMaterialsRolesDTO> rolesDTO = new ArrayList<ToolsAndMaterialsRolesDTO>();
	
	
	public ToolsAndMaterialsRolesDTO(){}
	
	public ToolsAndMaterialsRolesDTO(ToolAndMaterial toolAndMaterial,List<Role> unitOfLearningroles){
		if(toolAndMaterial instanceof ForumService){
			forumServiceConstructor(toolAndMaterial, unitOfLearningroles);
		}else if(toolAndMaterial instanceof AnnouncementService){
			announcementServiceConstructor(toolAndMaterial, unitOfLearningroles);
		} else if(toolAndMaterial instanceof ChatService){
			chatServiceConstructor(toolAndMaterial, unitOfLearningroles);
		} else{
			sentMailConstructor(toolAndMaterial, unitOfLearningroles);
		}
	}

	private void sentMailConstructor(ToolAndMaterial toolAndMaterial,
			List<Role> unitOfLearningroles) {
		List<Role> recipients = ((SentMailService) toolAndMaterial).getRecipients();
		ToolsAndMaterialsRolesDTO toolsAndMaterialsRolesDTO;
		if(recipients.isEmpty() ){
			for(Role role : unitOfLearningroles){
				toolsAndMaterialsRolesDTO = new ToolsAndMaterialsRolesDTO();
				toolsAndMaterialsRolesDTO.setId(role.getId());
				toolsAndMaterialsRolesDTO.setRoleName(role.getText());
				toolsAndMaterialsRolesDTO.setRecipient(false);
				roles.put(role.getId(), toolsAndMaterialsRolesDTO);
			}
			
		}

		if(!recipients.isEmpty()){
			for(Role role : recipients){
				toolsAndMaterialsRolesDTO = new ToolsAndMaterialsRolesDTO();
				toolsAndMaterialsRolesDTO.setRoleName(role.getText());
				toolsAndMaterialsRolesDTO.setId(role.getId());
				toolsAndMaterialsRolesDTO.setRecipient(true);
				roles.put(role.getId(), toolsAndMaterialsRolesDTO);
			}
			for(int i =0; i<unitOfLearningroles.size(); i++){
				if(!roles.containsKey(unitOfLearningroles.get(i).getId())){
					toolsAndMaterialsRolesDTO = new ToolsAndMaterialsRolesDTO();
					toolsAndMaterialsRolesDTO.setId(unitOfLearningroles.get(i).getId());
					toolsAndMaterialsRolesDTO.setRoleName(unitOfLearningroles.get(i).getText());
					toolsAndMaterialsRolesDTO.setRecipient(false);
					roles.put(unitOfLearningroles.get(i).getId(), toolsAndMaterialsRolesDTO);
				}
			}
		}
		Iterator<ToolsAndMaterialsRolesDTO> it = roles.values().iterator();
		while(it.hasNext()){
			rolesDTO.add((ToolsAndMaterialsRolesDTO) it.next());
		}
	}

	private void chatServiceConstructor(ToolAndMaterial toolAndMaterial,
			List<Role> unitOfLearningroles) {
		List<Role> observers = ((ChatService) toolAndMaterial).getObservers();
		List<Role> participants = ((ChatService) toolAndMaterial).getParticipants();
		ToolsAndMaterialsRolesDTO toolsAndMaterialsRolesDTO;
		if(observers.isEmpty() && participants.isEmpty()){
			for(Role role : unitOfLearningroles){
				toolsAndMaterialsRolesDTO = new ToolsAndMaterialsRolesDTO();
				toolsAndMaterialsRolesDTO.setId(role.getId());
				toolsAndMaterialsRolesDTO.setRoleName(role.getText());
				toolsAndMaterialsRolesDTO.setObserver(false);
				toolsAndMaterialsRolesDTO.setParticipant(false);
				roles.put(role.getId(), toolsAndMaterialsRolesDTO);
			}
			
		}
		for(Role role : observers){
			toolsAndMaterialsRolesDTO = new ToolsAndMaterialsRolesDTO();
			toolsAndMaterialsRolesDTO.setRoleName(role.getText());
			toolsAndMaterialsRolesDTO.setId(role.getId());
			toolsAndMaterialsRolesDTO.setObserver(true);
			toolsAndMaterialsRolesDTO.setParticipant(false);
			roles.put(role.getId(), toolsAndMaterialsRolesDTO);
		}
		for(Role role : participants){
			if(roles.containsKey(role.getId())){
				toolsAndMaterialsRolesDTO = roles.get(role.getId());
				toolsAndMaterialsRolesDTO.setParticipant(true);
				roles.put(role.getId(), toolsAndMaterialsRolesDTO);
			}else{
				ToolsAndMaterialsRolesDTO toolsAndMaterialsRolesDTO1 = new ToolsAndMaterialsRolesDTO();
				toolsAndMaterialsRolesDTO1.setId(role.getId());
				toolsAndMaterialsRolesDTO1.setRoleName(role.getText());
				toolsAndMaterialsRolesDTO1.setParticipant(true);
				toolsAndMaterialsRolesDTO1.setObserver(false);
				roles.put(role.getId(), toolsAndMaterialsRolesDTO1);
			}
			
		}
		if(!observers.isEmpty() || !participants.isEmpty()){
			for(int i =0; i<unitOfLearningroles.size(); i++){
				if(!roles.containsKey(unitOfLearningroles.get(i).getId())){
					toolsAndMaterialsRolesDTO = new ToolsAndMaterialsRolesDTO();
					toolsAndMaterialsRolesDTO.setId(unitOfLearningroles.get(i).getId());
					toolsAndMaterialsRolesDTO.setRoleName(unitOfLearningroles.get(i).getText());
					toolsAndMaterialsRolesDTO.setObserver(false);
					toolsAndMaterialsRolesDTO.setParticipant(false);
					roles.put(unitOfLearningroles.get(i).getId(), toolsAndMaterialsRolesDTO);
				}
			}
		}
		Iterator<ToolsAndMaterialsRolesDTO> it = roles.values().iterator();
		while(it.hasNext()){
			rolesDTO.add((ToolsAndMaterialsRolesDTO) it.next());
		}
	}

	private void announcementServiceConstructor(
			ToolAndMaterial toolAndMaterial, List<Role> unitOfLearningroles) {
		List<Role> observers = ((AnnouncementService) toolAndMaterial).getObservers();
		List<Role> participants = ((AnnouncementService) toolAndMaterial).getParticipants();
		ToolsAndMaterialsRolesDTO toolsAndMaterialsRolesDTO;
		if(observers.isEmpty() && participants.isEmpty()){
			for(Role role : unitOfLearningroles){
				toolsAndMaterialsRolesDTO = new ToolsAndMaterialsRolesDTO();
				toolsAndMaterialsRolesDTO.setId(role.getId());
				toolsAndMaterialsRolesDTO.setRoleName(role.getText());
				toolsAndMaterialsRolesDTO.setObserver(false);
				toolsAndMaterialsRolesDTO.setParticipant(false);
				roles.put(role.getId(), toolsAndMaterialsRolesDTO);
			}
			
		}
		for(Role role : observers){
			toolsAndMaterialsRolesDTO = new ToolsAndMaterialsRolesDTO();
			toolsAndMaterialsRolesDTO.setRoleName(role.getText());
			toolsAndMaterialsRolesDTO.setId(role.getId());
			toolsAndMaterialsRolesDTO.setObserver(true);
			toolsAndMaterialsRolesDTO.setParticipant(false);
			roles.put(role.getId(), toolsAndMaterialsRolesDTO);
		}
		for(Role role : participants){
			if(roles.containsKey(role.getId())){
				toolsAndMaterialsRolesDTO = roles.get(role.getId());
				toolsAndMaterialsRolesDTO.setParticipant(true);
				roles.put(role.getId(), toolsAndMaterialsRolesDTO);
			}else{
				ToolsAndMaterialsRolesDTO toolsAndMaterialsRolesDTO1 = new ToolsAndMaterialsRolesDTO();
				toolsAndMaterialsRolesDTO1.setId(role.getId());
				toolsAndMaterialsRolesDTO1.setRoleName(role.getText());
				toolsAndMaterialsRolesDTO1.setParticipant(true);
				toolsAndMaterialsRolesDTO1.setObserver(false);
				roles.put(role.getId(), toolsAndMaterialsRolesDTO1);
			}
			
		}
		if(!observers.isEmpty() || !participants.isEmpty()){
			for(int i =0; i<unitOfLearningroles.size(); i++){
				if(!roles.containsKey(unitOfLearningroles.get(i).getId())){
					toolsAndMaterialsRolesDTO = new ToolsAndMaterialsRolesDTO();
					toolsAndMaterialsRolesDTO.setId(unitOfLearningroles.get(i).getId());
					toolsAndMaterialsRolesDTO.setRoleName(unitOfLearningroles.get(i).getText());
					toolsAndMaterialsRolesDTO.setObserver(false);
					toolsAndMaterialsRolesDTO.setParticipant(false);
					roles.put(unitOfLearningroles.get(i).getId(), toolsAndMaterialsRolesDTO);
				}
			}
		}
		Iterator<ToolsAndMaterialsRolesDTO> it = roles.values().iterator();
		while(it.hasNext()){
			rolesDTO.add((ToolsAndMaterialsRolesDTO) it.next());
		}
	}

	private void forumServiceConstructor(ToolAndMaterial toolAndMaterial,
			List<Role> unitOfLearningroles) {
		List<Role> observers = ((ForumService) toolAndMaterial).getObservers();
		List<Role> participants = ((ForumService) toolAndMaterial).getParticipants();
		ToolsAndMaterialsRolesDTO toolsAndMaterialsRolesDTO;
		if(observers.isEmpty() && participants.isEmpty()){
			for(Role role : unitOfLearningroles){
				toolsAndMaterialsRolesDTO = new ToolsAndMaterialsRolesDTO();
				toolsAndMaterialsRolesDTO.setId(role.getId());
				toolsAndMaterialsRolesDTO.setRoleName(role.getText());
				toolsAndMaterialsRolesDTO.setObserver(false);
				toolsAndMaterialsRolesDTO.setParticipant(false);
				roles.put(role.getId(), toolsAndMaterialsRolesDTO);
			}
			
		}
		for(Role role : observers){
			toolsAndMaterialsRolesDTO = new ToolsAndMaterialsRolesDTO();
			toolsAndMaterialsRolesDTO.setRoleName(role.getText());
			toolsAndMaterialsRolesDTO.setId(role.getId());
			toolsAndMaterialsRolesDTO.setObserver(true);
			toolsAndMaterialsRolesDTO.setParticipant(false);
			roles.put(role.getId(), toolsAndMaterialsRolesDTO);
		}
		for(Role role : participants){
			if(roles.containsKey(role.getId())){
				toolsAndMaterialsRolesDTO = roles.get(role.getId());
				toolsAndMaterialsRolesDTO.setParticipant(true);
				roles.put(role.getId(), toolsAndMaterialsRolesDTO);
			}else{
				ToolsAndMaterialsRolesDTO toolsAndMaterialsRolesDTO1 = new ToolsAndMaterialsRolesDTO();
				toolsAndMaterialsRolesDTO1.setId(role.getId());
				toolsAndMaterialsRolesDTO1.setRoleName(role.getText());
				toolsAndMaterialsRolesDTO1.setParticipant(true);
				toolsAndMaterialsRolesDTO1.setObserver(false);
				roles.put(role.getId(), toolsAndMaterialsRolesDTO1);
			}
			
		}
		if(!observers.isEmpty() || !participants.isEmpty()){
			for(int i =0; i<unitOfLearningroles.size(); i++){
				if(!roles.containsKey(unitOfLearningroles.get(i).getId())){
					toolsAndMaterialsRolesDTO = new ToolsAndMaterialsRolesDTO();
					toolsAndMaterialsRolesDTO.setId(unitOfLearningroles.get(i).getId());
					toolsAndMaterialsRolesDTO.setRoleName(unitOfLearningroles.get(i).getText());
					toolsAndMaterialsRolesDTO.setObserver(false);
					toolsAndMaterialsRolesDTO.setParticipant(false);
					roles.put(unitOfLearningroles.get(i).getId(), toolsAndMaterialsRolesDTO);
				}
			}
		}
		Iterator<ToolsAndMaterialsRolesDTO> it = roles.values().iterator();
		while(it.hasNext()){
			rolesDTO.add((ToolsAndMaterialsRolesDTO) it.next());
		}
	}
	
	public String getRoleName() {
		return roleName;
	}
	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}
	public boolean isObserver() {
		return observer;
	}
	public void setObserver(boolean observer) {
		this.observer = observer;
	}
	public boolean isParticipant() {
		return participant;
	}
	public void setParticipant(boolean participant) {
		this.participant = participant;
	}

	public HashMap<Integer, ToolsAndMaterialsRolesDTO> getRoles() {
		return roles;
	}

	public void setRoles(HashMap<Integer, ToolsAndMaterialsRolesDTO> roles) {
		this.roles = roles;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public List<ToolsAndMaterialsRolesDTO> getRolesDTO() {
		return rolesDTO;
	}

	public void setRolesDTO(List<ToolsAndMaterialsRolesDTO> rolesDTO) {
		this.rolesDTO = rolesDTO;
	}

	public boolean isRecipient() {
		return recipient;
	}

	public void setRecipient(boolean recipient) {
		this.recipient = recipient;
	}

}
